package com.example.llm.learn.api.controller;

import com.example.llm.learn.api.dto.CommonResponse;
import com.example.llm.learn.api.dto.PageDTO;
import com.example.llm.learn.api.dto.TradeMarkDTO;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@CrossOrigin(origins = {"http://localhost:5173", "http://127.0.0.1:5500"})
@RestController
public class TradeMarkController implements InitializingBean {

    private List<TradeMarkDTO> list = new LinkedList<>();

    @Override
    public void afterPropertiesSet() throws Exception {
        list.add(new TradeMarkDTO().setId(1L).setName("华为").setLogo("https://www.huawei.com/-/media/hcomponent-header/1.0.1.20250709175216/component/img/huawei_logo.png"));
        list.add(new TradeMarkDTO().setId(2L).setName("苹果").setLogo("https://www.apple.com.cn/home/images/wechat/open_graph_logo.png"));
        list.add(new TradeMarkDTO().setId(3L).setName("德州仪器").setLogo("https://www.ti.com/etc/designs/ti/images/ui/ic-logo.svg"));
        list.add(new TradeMarkDTO().setId(4L).setName("洛克希德马丁1").setLogo("https://www.lockheedmartin.com/content/dam/lockheed-martin/general/lm-logo.svg"));
        list.add(new TradeMarkDTO().setId(5L).setName("洛克希德马丁2").setLogo("https://www.lockheedmartin.com/content/dam/lockheed-martin/general/lm-logo.svg"));
        list.add(new TradeMarkDTO().setId(6L).setName("洛克希德马丁3").setLogo("https://www.lockheedmartin.com/content/dam/lockheed-martin/general/lm-logo.svg"));
        list.add(new TradeMarkDTO().setId(7L).setName("洛克希德马丁4").setLogo("https://www.lockheedmartin.com/content/dam/lockheed-martin/general/lm-logo.svg"));
        list.add(new TradeMarkDTO().setId(8L).setName("洛克希德马丁5").setLogo("https://www.lockheedmartin.com/content/dam/lockheed-martin/general/lm-logo.svg"));
        list.add(new TradeMarkDTO().setId(9L).setName("洛克希德马丁6").setLogo("https://www.lockheedmartin.com/content/dam/lockheed-martin/general/lm-logo.svg"));
        list.add(new TradeMarkDTO().setId(10L).setName("洛克希德马丁7").setLogo("https://www.lockheedmartin.com/content/dam/lockheed-martin/general/lm-logo.svg"));
        list.add(new TradeMarkDTO().setId(11L).setName("洛克希德马丁8").setLogo("https://www.lockheedmartin.com/content/dam/lockheed-martin/general/lm-logo.svg"));
        list.add(new TradeMarkDTO().setId(12L).setName("洛克希德马丁9").setLogo("https://www.lockheedmartin.com/content/dam/lockheed-martin/general/lm-logo.svg"));
        list.add(new TradeMarkDTO().setId(13L).setName("洛克希德马丁10").setLogo("https://www.lockheedmartin.com/content/dam/lockheed-martin/general/lm-logo.svg"));
        list.add(new TradeMarkDTO().setId(14L).setName("洛克希德马丁11").setLogo("https://www.lockheedmartin.com/content/dam/lockheed-martin/general/lm-logo.svg"));
        list.add(new TradeMarkDTO().setId(15L).setName("洛克希德马丁12").setLogo("https://www.lockheedmartin.com/content/dam/lockheed-martin/general/lm-logo.svg"));
        list.add(new TradeMarkDTO().setId(16L).setName("德州仪器13"));
        list.add(new TradeMarkDTO().setId(17L).setName("德州仪器14"));
        list.add(new TradeMarkDTO().setId(18L).setName("德州仪器15"));
        list.add(new TradeMarkDTO().setId(19L).setName("德州仪器16"));
        list.add(new TradeMarkDTO().setId(20L).setName("德州仪器17"));
        list.add(new TradeMarkDTO().setId(21L).setName("德州仪器18"));
        list.add(new TradeMarkDTO().setId(22L).setName("德州仪器19"));
    }

    @GetMapping(value = "/tradeMark/list")
    public CommonResponse<PageDTO<TradeMarkDTO>> list(@RequestParam(required = false, defaultValue = "1") int pageNo,
                                                      @RequestParam(required = false, defaultValue = "10") int pageSize) {
        PageDTO<TradeMarkDTO> page = new PageDTO<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setTotal(list.size());
        int skip = Math.min(list.size(), (pageNo - 1) * pageSize);
        page.setList(list.stream().skip(skip).limit(pageSize).collect(Collectors.toList()));
        return CommonResponse.ok(page);
    }

    @PostMapping(value = "/tradeMark/add")
    public CommonResponse<TradeMarkDTO> add(@RequestBody TradeMarkDTO tradeMark) {
        tradeMark.setId(System.currentTimeMillis());
        list.add(tradeMark);
        return CommonResponse.ok(tradeMark);
    }

    @PostMapping(value = "/tradeMark/edit")
    public CommonResponse<TradeMarkDTO> edit(@RequestBody TradeMarkDTO tradeMarkDTO) {
        TradeMarkDTO tradeMark = findById(tradeMarkDTO.getId());
        if (Objects.isNull(tradeMark)) {
            return CommonResponse.error500("不存在的记录");
        }
        tradeMark.setName(tradeMarkDTO.getName());
        tradeMark.setLogo(tradeMarkDTO.getLogo());
        return CommonResponse.ok(tradeMark);
    }

    @DeleteMapping(value = "/tradeMark/delete")
    public CommonResponse<Boolean> delete(@RequestParam Long id) {
        TradeMarkDTO tradeMark = findById(id);
        if (Objects.isNull(tradeMark)) {
            return CommonResponse.error500("不存在的记录");
        }
        list = list.stream().filter(item -> !item.getId().equals(id)).collect(Collectors.toList());
        return CommonResponse.ok(Boolean.TRUE);
    }

    @GetMapping(value = "/tradeMark/{id}")
    public CommonResponse<TradeMarkDTO> get(@PathVariable(value = "id") Long id) {
        TradeMarkDTO tradeMark = findById(id);
        if (Objects.isNull(tradeMark)) {
            return CommonResponse.error500("不存在的记录");
        }
        return CommonResponse.ok(tradeMark);
    }

    private TradeMarkDTO findById(Long id) {
        Iterator<TradeMarkDTO> iterator = list.iterator();
        while (iterator.hasNext()) {
            TradeMarkDTO tradeMark = iterator.next();
            if (tradeMark.getId().equals(id)) {
                return tradeMark;
            }
        }
        return null;
    }
}
